package com.wxy.netty;

import com.wxy.网络编程.Server;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

public class handler {
    public static void main(String[] args) {
        EventLoopGroup group = new DefaultEventLoopGroup();
        new ServerBootstrap()
                .group(new NioEventLoopGroup(), new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {

                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        nioSocketChannel.pipeline().addLast(new StringDecoder());   //addLast是将自定义处理器添加在最后一个处理器之前
                        nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("1");
                                System.out.println(msg.toString()+"\n");
                                super.channelRead(ctx, msg);
                            }
                        }).addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("2");
                                System.out.println(msg+"\n");
                                super.channelRead(ctx, msg);
                            }
                        }).addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("3");
                                System.out.println(msg+"\n");
                                //ctx会在此处理器基础上向前找出站处理器 添加在入站处理器后的出战处理器不起作用
                                //ctx.writeAndFlush(ctx.alloc().buffer().writeBytes("hello".getBytes()));
                                //必须调用writeAndFlush方法才能触发出站处理器
                                nioSocketChannel.writeAndFlush("hello");
                            }
                        }).addLast(new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("4: out " + msg);
                                super.write(ctx, msg, promise);
                            }
                        }).addLast(new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("5: out " + msg);
                                super.write(ctx, msg, promise);
                            }
                        }).addLast(new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("6: out " + msg);
                                super.write(ctx, msg, promise);
                            }
                        });
                    }
                })
                .bind("127.0.0.1",8888);
    }
}
